package com.sky.common.interceptor;

import com.sky.common.context.BaseContext;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * ClassName: com.sky.interceptor
 * Datetime: 2024/9/30 下午8:15
 * Description:
 *
 * @author 汪佳泉
 * @version 1.0
 * @since 1.0
 */
public class UserInfoInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userInfo = request.getHeader("user-info");
        if(StringUtils.isNotBlank(userInfo)){
            BaseContext.setCurrentId(Long.valueOf(userInfo));
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 移除用户
        BaseContext.removeCurrentId();
    }
}
